package com.powergisol.gis.controller;

import com.powergisol.gis.service.TabDataMenuService;
import com.powergisol.gis.util.Result;
import com.powergisol.gis.vo.TabDataMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;

/**
 * Date: 2019/7/9  10:15
 * Description:
 */
@RestController
@Api(description = "数据类型" ,value = "数据类型")
public class TabDataMenuController {
    @Autowired
    private TabDataMenuService tabDataMenuService;

    /**
     * 数据类型列表
     */
    @PostMapping("tabDataMenuList")
    @ApiOperation(value = "数据类型列表", notes = "数据类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "userId", value = "用户id(userId 和 userName 必传一个)", defaultValue = "0", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "userName", value = "用户名称", defaultValue = "0", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "数据类型id", defaultValue = "0", required = true, paramType = "query")
    })
    public Result tabDataMenuList(TabDataMenuVo vo, Principal p){
        Result result = new Result();
//        if(null == vo.getUserId()&& null ==vo.getUserName()){
//            return result.failure(-1,"userId和userName不能全部为空");
//        }
        if(StringUtils.isEmpty(vo.getUserName())){
            vo.setUserName(p.getName());
        }
        List<TabDataMenuVo> menuVoList = tabDataMenuService.getTabDataMenuByPId(vo);
        result.setData(menuVoList);
        return result;
    }
    /**
     * 增加数据类型
     */
    @PostMapping("saveTabDataMenu")
    @ApiOperation(value = "增加数据类型", notes = "增加数据类型")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "userId", value = "用户id", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "userName", value = "用户名", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "pId", value = "父级id", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "dataName", value = "名称", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "icon", value = "图标", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "sort", value = "排序", defaultValue = "0", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "Integer", name = "isSub", value = "是否有下一级(0:无，1有)", defaultValue = "0", required = true, paramType = "query")
    })
    public Result saveTabDataMenu(TabDataMenuVo vo, Principal p){
        if(StringUtils.isEmpty(vo.getUserName())){
            vo.setUserName(p.getName());
        }
        Result result = tabDataMenuService.saveTabDataMenu(vo);
        return result;
    }
    /**
     * 删除数据类型
     */
    @PostMapping("deleteTabDataMenu")
    @ApiOperation(value = "删除数据类型", notes = "删除数据类型")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "数据类型id", defaultValue = "0", required = true, paramType = "query")
    })
    public Result deleteTabDataMenu(Integer id){
        Result result = tabDataMenuService.deleteTabDataMenu(id);
        return result;
    }

}
